package com.qezhhnjy.flink.job;

import com.qezhhnjy.flink.mapper.MyFlatMapper;
import com.qezhhnjy.flink.source.MySource;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author zhaoyangfu
 * @date 2021/8/24-15:12
 */
public class WordCount {

    public static void stream(String[] args) throws Exception {
        // 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> data = env.addSource(new MySource());
        data.flatMap(new MyFlatMapper())
                .keyBy((KeySelector<Tuple2<String, Integer>, String>) value -> value.f0)
                .sum(1).print();
        env.execute("word count");
    }
}

